<template>
  <el-dialog
    title="新增员工"
    :visible="isShowAddEmployees"
    width="50%"
    @close="btnCancel"
  >
    <!-- 表单 -->
    <el-form ref="formDataRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="(item, index) in Employees.hireType" :key="index" :label="item.value" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDepartmentTree" />
        <el-tree v-if="showTree" v-loading="loading" :data="treeData" :props="defaultProps" @node-click="onNodeclick"></el-tree>
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTimecorrectionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>

    <el-row slot="footer" type="flex" justify="center" class="dialog-footer">
      <div>
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>

      </div>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartmentTree } from '@/api/departments'
import { transListToTreeData } from '@/utils'
import Employees from '@/api/constant/employees'
import { addEmployess } from '@/api/employees'
export default {
  name: 'Addempliyees',
  components: {},
  props: {
    isShowAddEmployees: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      Employees, // 引入的数据
      defaultProps: { label: 'name' },
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      treeData: [], // 树形企业部门
      loading: false, // 树形结构的loading时间
      showTree: false // 是否显示树形结构
    }
  },
  created() {

  },
  methods: {
    // 树形结构的节点点击事件
    onNodeclick(data) {
      console.log(data)
      this.formData.departmentName = data.name
      this.showTree = false
    },
    // onBlur() {
    //   this.showTree = false
    // },
    // 获取企业部门
    async  getDepartmentTree() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartmentTree()
      this.treeData = transListToTreeData(depts)
      this.loading = false
    },
    // 弹出框关闭事件
    btnCancel() {
      this.formData = this.$options.data().formData
      this.$refs.formDataRef.resetFields()
      //   this.dialogVisible = false
      this.$emit('update:isShowAddEmployees', false) // 向父组件发送信息 关闭弹出层组件
    },
    async btnOk() {
      try {
        await this.$refs.formDataRef.validate()
        await addEmployess(this.formData)
        this.$message({
          message: '添加成功',
          type: 'success',
          showClose: true,
          duration: 3000
        })
        // this.$emit('update:isShowAddEmployees', false)
        this.$parent.isShowAddEmployees = false
        // this.$parent.getEmployeesList() // 获取员工列表
        this.$emit('getEmployeesList') // 获取员工列表
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
